﻿using System;
using System.Collections.Generic;
using Kaio.Core;


namespace MvcForum.Net.Bussiness
{
    public class TopicRepo
    {
        public static List<Topic> List(int forumId, bool isModerator, int pageIndex, int pageSize, string orderBy, string sortDir, out  int totalRows)
        {
            var _sp = MainDB.Instant.Topic_List(forumId, isModerator, pageIndex, pageSize, orderBy, sortDir);

            var _list = _sp.ExecuteTypedList<Topic>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);
            return _list;
        }


        public static List<Topic> List(int forumId, bool isModerator, int pageIndex, int pageSize, out  int totalRows)
        {

            return List(forumId, isModerator, pageIndex, pageSize, "LastPost", "DESC", out totalRows);
        }


        public static List<Topic> List(int topicId, string postIds)
        {

            return MainDB.Instant.Topic_QuoteList(topicId, postIds).ExecuteTypedList<Topic>();

        }



        /* public static void Add(int? topicId, string topicName, int forumId, int? pollId, string icon, string message, int? replyTo, int priority, DateTime? priorityDate,
            bool isApproved, string editedBy, string remoteUrl, string userName, int userId, string ip, out int Id)
         {
             
         }*/
        public static void Save(Topic t, out int id)
        {


            var _sp =
                MainDB.Instant.Topic_Save(t.TopicId, t.TopicName, t.ForumId, t.Icon, t.Message, t.ReplyTo,
                                          t.Priority, t.PriorityExpires, t.IsApproved, t.EditedBy,t.Reason, t.RemoteUrl, t.UserName,
                                          t.UserId, t.IP, t.Tags, t.PollId,t.IncreasePosts);

            _sp.Execute();

            id = Convert.ToInt32(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);

            // Clear Cache Update Last Post
            CacheHelper.Remove(CacheKeys.FORUM_ALL);
        }


        public static Topic FindPost(int topicId, string action, out int totalRows)
        {
            var _sp = MainDB.Instant.Topic_FindPost(topicId, action);

            var _list = _sp.ExecuteTypedList<Topic>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);

            return _list.Count > 0 ? _list[0] : null;
        }


        public static Topic Detail(int topicId)
        {
            var _list = MainDB.Instant.Topic_Detail(topicId).ExecuteTypedList<Topic>();
            return _list.Count > 0 ? _list[0] : null;
        }

        public static Topic Info(int id)
        {
            return Detail(id);
        }


        /* public static Topic OwnDetail(int id, bool mod, int userId)
         {
             var _list = MainDB.Instant.Topic_OwnDetail(id, mod, userId).ExecuteTypedList<Topic>();
             return _list.Count > 0 ? _list[0] : null;
         }*/



        public static List<Topic> Thread(bool isMod, int topicId, int pageIndex, int pageSize, out  int totalRows, out List<Like> likes)
        {

            var _sp = MainDB.Instant.Thread_List(isMod, topicId, pageIndex, pageSize);

            var _list = _sp.ExecuteTypedList<Topic>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);

            likes = LikeRepo.List(topicId, pageIndex, pageSize);

            return _list;
        }





        public static void ToggleLock(int id)
        {

            MainDB.Instant.Topic_ToggleLock(id).Execute();

        }

        public static void ToggleApproved(int id)
        {

            MainDB.Instant.Topic_ToggleApproved(id).Execute();

        }

        public static void Del(int id,int fid)
        {
            Dels(id.ToString(),fid);
        }

        public static void Dels(string ids,int forumId)
        {

            CacheHelper.Remove(CacheKeys.FORUM_ALL);

            MainDB.Instant.Topic_Delete(ids, forumId).Execute();
        }



        #region Search

        public static List<Topic> QuickSearch(int forumId,bool isMod, string kw, short resultType, int maxResults)
        {
            return MainDB.Instant.Topic_SearchQuick(forumId,isMod, kw, resultType, maxResults).ExecuteTypedList<Topic>();
        }



        public static List<Topic> SearchAdvanced(string kwd, string username, string sql)
        {

            return MainDB.Instant.Topic_SearchAdvanced(kwd, username, sql).ExecuteTypedList<Topic>();

            //return new CodingHorror(sql, args).ExecuteTypedList<Topic>();


        }

        /*  public static List<Topic> SearchAdvanced(string sql, params object[] args)
          {
              //return new CodingHorror(sql, args).ExecuteTypedList<Topic>();


          }*/


        public static List<Topic> GetNewPosts(short resultType, int maxResults, DateTime lastSearch)
        {
            return MainDB.Instant.Topic_GetNewPosts(resultType, maxResults, lastSearch).ExecuteTypedList<Topic>();
        }


        public static List<Topic> SearchEngineSitemap(int pageIndex, int pageSize)
        {
            return MainDB.Instant.Topic_SearchEngineSitemap(pageIndex, pageSize).ExecuteTypedList<Topic>();
        }


        public static List<Topic> Feed(int forumId, int pageSize)
        {
            return MainDB.Instant.Topic_Feed(forumId, pageSize).ExecuteTypedList<Topic>();
        }


        #region Moderator

        public static void Mod_Queue(string ids,int fid, bool b, bool allowTopic, bool allowPost)
        {
            MainDB.Instant.Topic_Mod_Queue(ids,fid, allowTopic, allowPost, b).Execute();
        }


        public static void Mod_ToggleLock(string ids, bool val)
        {
            MainDB.Instant.Topic_Mod_ToggleLock(ids, val).Execute();
        }


        public static void Mod_Delete(string ids,int fid, bool allowTopic, bool allowPost)
        {
            MainDB.Instant.Topic_Mod_Delete(ids,fid, allowTopic, allowPost).Execute();
        }


        public static void Mod_ToggleSticky(string ids, short priority, DateTime? expires)
        {
            MainDB.Instant.Topic_Mod_ToggleSticky(ids, priority, expires).Execute();
        }

        public static void Mod_MoveToForum(string ids, int fid, bool allowRedirect, DateTime? expires)
        {
            MainDB.Instant.Topic_Mod_MoveToForum(ids, fid, allowRedirect, expires).Execute();
        }

        public static void Mod_UnMove(string ids)
        {
            MainDB.Instant.Topic_Mod_MoveToForum(ids, -1, false, null).Execute();
        }

        #endregion

        #endregion

    }
}
